package ru.amse.ilyin.gui;

import java.awt.BorderLayout;

import javax.swing.JFrame;

import ru.amse.ilyin.provider.DiagramProvider;

/**
 * Main window of the application.
 *
 * @author Alexander Ilyin
 */
public class MainWindow extends JFrame {
	private final DrawingComponent packageDiagram;
	
	private final DrawingComponent classDiagram;
	
	private boolean showingPackageDiagram;
	
	/**
	 * Creates main window.
	 *
	 * @param controller controller
	 */
	public MainWindow(DiagramProvider controller) {
		super("Bytecode analyzer");

		packageDiagram = new DrawingComponent(controller.getPackageDiagram(null));
		classDiagram = new DrawingComponent(controller.getClassDiagram());
		showingPackageDiagram = true;
		
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		setSize(500, 400);
		
        setJMenuBar(MenuFactory.createMenu());
        getContentPane().add(
        		ToolbarFactory.createToolbar(this), 
        		BorderLayout.NORTH
        );
        getContentPane().add(packageDiagram);
        getContentPane().add(new StatusBar(), BorderLayout.SOUTH);
		
		setVisible(true);
	}
	
	/**
	 * Shows class diagrams, if package diagram has been displayed.
	 * Otherwise, does nothing.
	 */
	public void showClassDiagrams() {
		if (showingPackageDiagram == false) {
			return;
		}
	
		showingPackageDiagram = false;
		getContentPane().remove(packageDiagram);
		getContentPane().add(classDiagram);
		getContentPane().validate();
		repaint();
	}
	
	/**
	 * Shows package diagram, if class diagrams have been displayed.
	 * Otherwise, does nothing.
	 */
	public void showPackageDiagram() {
		if (showingPackageDiagram == true) {
			return;
		}
		
		showingPackageDiagram = true;
		getContentPane().remove(classDiagram);
		getContentPane().add(packageDiagram);
		getContentPane().validate();
		repaint();
	}
}
